﻿using DailyApp.WPF.DTOs;
using DailyApp.WPF.HttpClients;
using Newtonsoft.Json;
using Prism.Commands;
using Prism.Mvvm;
using RestSharp;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;

namespace DailyApp.WPF.ViewModels
{
    /// <summary>
    /// 备忘录视图模型
    /// </summary>
    class MemoUCviewModel : BindableBase
    {
        private readonly HttpRestClient httpRestClient;


        #region 委托命令

        //右侧弹窗
        public DelegateCommand ShowOrHidenRightMenuCmd { get; set; }

        //查询备忘录信息
        public DelegateCommand QueryMemoInfoListCmd { get; set; }

        //添加备忘信息
        public DelegateCommand AddMemoInfoCmd { get; set; }

        //删除备忘信息
        public DelegateCommand<MemoInfoDTO> DeleteMemoInfoCmd { get; set; }


        #endregion


        #region 属性

        /// <summary>
        /// 备忘录事项集合
        /// </summary>
        private List<MemoInfoDTO> _memoInfoList;
        public List<MemoInfoDTO> MemoInfoList
        {
            get { return _memoInfoList; }
            set
            {
                _memoInfoList = value;
                RaisePropertyChanged();
            }
        }

        /// <summary>
        /// 右侧弹窗控制属性
        /// </summary>
        private bool _isRightDrawerOpen = false;
        public bool IsRightDrawerOpen
        {
            get { return _isRightDrawerOpen; }
            set
            {
                _isRightDrawerOpen = value;
                RaisePropertyChanged();
            }
        }

        /// <summary>
        /// 条件查询待办事项标题
        /// </summary>
        private string _searchWaitTitle;
        public string SearchWaitTitle
        {
            get { return _searchWaitTitle; }
            set { _searchWaitTitle = value; }
        }


        /// <summary>
        /// 条件查询待办事项状态
        /// </summary>
        private int _searchWaitStatus = 2;
        public int SearchWaitStatus
        {
            get { return _searchWaitStatus; }
            set
            {
                _searchWaitStatus = value;
                QueryMemoInfoListFunc();
                RaisePropertyChanged();
            }
        }

        /// <summary>
        /// 添加备忘信息
        /// </summary>
        private MemoInfoDTO? _addMemoInfo;
        public MemoInfoDTO? AddMemoInfo
        {
            get
            {
                if (_addMemoInfo == null)
                    _addMemoInfo = new MemoInfoDTO();
                return _addMemoInfo;
            }
            set
            {

                _addMemoInfo = value;
                RaisePropertyChanged();
            }
        }


        #endregion


        /// <summary>
        /// 构造函数
        /// </summary>
        public MemoUCviewModel(HttpRestClient _httpRestClient)
        {
            httpRestClient = _httpRestClient;

            //委托命令实现
            ShowOrHidenRightMenuCmd = new DelegateCommand(ShowOrHidenRightMenuFunc);
            QueryMemoInfoListCmd = new DelegateCommand(QueryMemoInfoListFunc);
            AddMemoInfoCmd = new DelegateCommand(AddMemoInfoFunc);
            DeleteMemoInfoCmd = new DelegateCommand<MemoInfoDTO>(DeleteMemoInfoFunc);

            //更新
            QueryMemoInfoListFunc();
        }


        #region 具体方法

        /// <summary>
        /// 初始化备忘录信息卡的方法
        /// </summary>
        private async void QueryMemoInfoListFunc()
        {
            MemoInfoList = new List<MemoInfoDTO>();

            ApiRequest apiRequest = new ApiRequest();
            apiRequest.Method = Method.GET;
            apiRequest.Route = $"Memo/QueryMemoInfo?title={SearchWaitTitle}&status={SearchWaitStatus}";

            var res = await httpRestClient.ExecuteAsync(apiRequest);

            if (res.ResultCode == 1)
            {
                MemoInfoList = JsonConvert.DeserializeObject<List<MemoInfoDTO>>(res.ResultData.ToString());
            }
            else
            {
                MessageBox.Show("error");
            }
        }


        /// <summary>
        /// 右侧弹窗
        /// </summary>
        private void ShowOrHidenRightMenuFunc()
        {
            if (!IsRightDrawerOpen)
            {
                IsRightDrawerOpen = true;
            }
            else
                IsRightDrawerOpen = false;
        }


        /// <summary>
        /// 添加待办事项
        /// </summary>
        private async void AddMemoInfoFunc()
        {
            ApiRequest apiRequest = new ApiRequest();

            //调用webapi
            apiRequest.Method = Method.POST;
            apiRequest.Route = "Memo/AddMemoInfo";
            apiRequest.Parameters = AddMemoInfo;

            var response = await httpRestClient.ExecuteAsync(apiRequest);

            if (response.ResultCode == 1)
            {
                //刷新
                QueryMemoInfoListFunc();
                IsRightDrawerOpen = false;//关闭右侧菜单
                MessageBox.Show("添加成功!");
            }
            else
            {
                MessageBox.Show("添加失败!");
            }
        }


        /// <summary>
        /// 删除待办事项
        /// </summary>
        private async void DeleteMemoInfoFunc(MemoInfoDTO delMemoInfo)
        {
            var apiRequest = new ApiRequest();
            apiRequest.Method = Method.DELETE;
            apiRequest.Route = $"Memo/DeleteMemoInfo?delId={delMemoInfo.MemoId}";

            var res = await httpRestClient.ExecuteAsync(apiRequest);

            if (res.ResultCode == 1)
            {
                //更新
                QueryMemoInfoListFunc();
                MessageBox.Show("删除成功！");
            }
            else
            {
                MessageBox.Show("服务器忙...");
            }
        }


        #endregion
    }
}
